/**
 * 
 */
package string.passed2;

import java.util.Arrays;
import java.util.HashMap;


/**
 * @author xyyi
 *
 */
public class MinimumWindowSubstring {

	/**
	Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

	For example,
	S = "ADOBECODEBANC"
	T = "ABC"

	Minimum window is "BANC".

	Note:
	If there is no such window in S that covers all characters in T, return the empty string "".

	If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
	 */
    public String minWindow1(String s, String t) {
        if (s.isEmpty() || t.isEmpty() || s.length() < t.length()) {
        	return "";
        }
        
        int[] counters = new int[256];
        for (char ch : t.toCharArray()) {
        	counters[ch]++;
        }
        
        int charsToFind = t.length();
        int minStart = -1;
        int minEnd = Integer.MAX_VALUE - 1;
        int start = 0;
        for (int i = 0; i < s.length(); i++) {
        	int curr = s.charAt(i);
        	if (counters[curr] > 0) {
        		charsToFind--;
        	}
        	counters[curr]--;
        	
        	while (charsToFind == 0) {
        		if (i - start + 1 <= minEnd - minStart ) {
        			minStart = start;
        			minEnd = i;
        		}
        		
        		int ch = s.charAt(start);
        		counters[ch]++;
        		if (counters[ch] > 0) {
        			charsToFind++;
        		}
        		start++;
        	}
        }
        
        return minStart < 0 ? "" : s.substring(minStart, minEnd + 1);
    }	
	
	public String minWindow(String s, String t) {
		if (s == null || t == null || s.isEmpty() || t.isEmpty()
				|| s.length() < t.length())
			return "";
		// counter the number of c
		HashMap<Character, Integer> counter = new HashMap<Character, Integer>();
		for (char c : t.toCharArray()) {
			if (counter.containsKey(c))
				counter.put(c, counter.get(c) + 1);
			else
				counter.put(c, 1);
		}

		String result = "";
		int sizeToFind = t.length();
		int minWinSize = Integer.MAX_VALUE;
		int start = 0;

		for (int i = 0; i < s.length(); i++) {
			char currChar = s.charAt(i);
			if (counter.containsKey(currChar)) {
				if (counter.get(currChar) > 0)
					sizeToFind--;
				counter.put(currChar, counter.get(currChar) - 1);
			} else {
				counter.put(currChar, -1);
			}

			while (sizeToFind == 0) {
				int size = i - start + 1;
				if (size < minWinSize) {
					minWinSize = size;
					result = s.substring(start, i + 1);
				}
				char startChar = s.charAt(start);
				counter.put(startChar, counter.get(startChar) + 1);
				if (counter.get(startChar) > 0)
					sizeToFind++;
				start++;
			}
		}
		return result;
	}

	public String minWindow0(String S, String T) {
		if (S.length() < T.length())
			return "";
		HashMap<Character, Integer> counter = new HashMap<Character, Integer>();
		for (char c : T.toCharArray()) {
			if (counter.containsKey(c)) {
				counter.put(c, counter.get(c) + 1);
			} else {
				counter.put(c, 0);
			}
		}

		int index = 0;
		int minSize = Integer.MIN_VALUE;
		String result = null;
		int size = T.length();
		for (int i = 0; i < S.length(); i++) {
			char c = S.charAt(i);
			if (counter.containsKey(c)) {
				if (counter.get(c) > 1)
					size--;
			} else {
				counter.put(c, 1);
			}

			counter.put(c, counter.get(c) - 1);

			while (size == 0) {
				if (i - index + 1 > minSize) {
					minSize = i - index + 1;
					result = S.substring(index, i + 1);
				}
				char sChar = S.charAt(index);
				counter.put(sChar, counter.get(sChar) + 1);
				if (counter.get(sChar) > 0)
					size++;
				index++;
			}
		}

		return result;
	}

	public String minWindow2(String S, String T) {

		int[] charsFoundInS = new int[256];
		int[] charsInT = new int[256];
		Arrays.fill(charsFoundInS, 0);
		Arrays.fill(charsInT, 0);

		for (int i = 0; i < T.length(); i++) {
			charsInT[T.charAt(i)]++;
		}

		int count = 0;
		int minWindowBegin = 0;
		int minWindowLength = Integer.MAX_VALUE;
		for (int begin = 0, end = 0; end < S.length(); end++) {
			if (charsInT[S.charAt(end)] > 0) {
				if (charsFoundInS[S.charAt(end)] < charsInT[S.charAt(end)]) {
					count++;
				}
				charsFoundInS[S.charAt(end)]++;

				if (count == T.length()) {
					while (charsInT[S.charAt(begin)] == 0
							|| charsFoundInS[S.charAt(begin)] > charsInT[S
									.charAt(begin)]) {
						if (charsFoundInS[S.charAt(begin)] > charsInT[S
								.charAt(begin)]) {
							charsFoundInS[S.charAt(begin)]--;
						}
						begin++;
					}

					if (minWindowLength > end - begin + 1) {
						minWindowBegin = begin;
						minWindowLength = end - begin + 1;
					}
				}
			}
		}

		return minWindowLength == Integer.MAX_VALUE ? "" : S.substring(
				minWindowBegin, minWindowBegin + minWindowLength);
	}	
	
	/**
	 * 
	 */
	public MinimumWindowSubstring() {

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// int x = "1".charAt(0); // String to Character to int
		// int y = 'A'; // Character to int
		// byte[] z = "B|".getBytes(); // String to bytes
		// System.out.println("1=" + x + " A=" + y + " B=" + z[0] + " |=" +
		// z[1]);

//		String s = "ADOBECODEBANC";
//		String t = "ABC";
		String s = "a";
		String t = "a";
		MinimumWindowSubstring mws = new MinimumWindowSubstring();
		System.out.printf("min windows sub is: %s", mws.minWindow1(s, t));
	}
}
